﻿#include "MEMSData.h"

#include "qdebug.h"

int MEMSData::minSize=sizeof ((char)SourceData)+sizeof(int)+sizeof ((char)DateTime)+sizeof (qint64);
MEMSData::MEMSData(QObject *parent) : QObject(parent)
{
  createTime=QDateTime::currentDateTime();
}

MEMSData::MEMSData(const MEMSData& mEMSData)
{
  setSourceData(QByteArray(mEMSData.getSourceData()));
  setCreateTime(mEMSData.getCreateTime());
}

QByteArray MEMSData::toByteArray() const
{
  qint64 UTC=createTime.isValid()?createTime.toSecsSinceEpoch():0;
  int scrDataLen=sourceData.size();
  char* temp=(char*)&scrDataLen;
  QByteArray data;
  data.append((char)(SourceData&0xff));
  data.append(temp,sizeof (int));
  data.append(sourceData);
  data.append((char)(DateTime&0xff));
  temp=(char*)&UTC;
  data.append(temp,sizeof (qint64));
  return data;
}

MEMSData MEMSData::fromByteArray(QByteArray data)
{
  MEMSData mEMSData;
  if(data.isEmpty())
    {
      return mEMSData;
    }
  if(data.size()<minSize)
    {
      return mEMSData;
    }
  char* dataPtr=data.data();
  int srcDataLen=data.size();
  if((SourceData&0xff)!=*dataPtr)
    {
      return mEMSData;
    }
  ++dataPtr;
  int dataLen=*(int*)(dataPtr);
  dataPtr+=4;
  if(srcDataLen<dataLen+minSize)
    {
      return mEMSData;
    }
  mEMSData.setSourceData(QByteArray(dataPtr,dataLen));
  dataPtr+=dataLen;
  if((DateTime&0xff)!=*dataPtr)
    {
      mEMSData.setSourceData(QByteArray());
      return mEMSData;
    }
  ++dataPtr;
  qint64 UTC=*(qint64*)dataPtr;
  QDateTime dateTime= QDateTime::fromSecsSinceEpoch(UTC);
  if(!dateTime.isValid())
    {
      mEMSData.setSourceData(QByteArray());
      return mEMSData;
    }
  mEMSData.setCreateTime(dateTime);
  return mEMSData;
}

QVector<MEMSData> MEMSData::fromByteArrayToList(QByteArray data)
{
  QVector<MEMSData> list;
  if(data.isEmpty())
    {
      return list;
    }
  if(data.size()<minSize)
    {
      return list;
    }
  char* dataPtr=data.data();
  int nowAt=0;
  int srcDataLen=data.size();
  while (nowAt<data.size())
    {
      MEMSData mEMSData;
      if((SourceData&0xff)!=*dataPtr)
        {
          return list;
        }
      ++dataPtr;
      ++nowAt;
      int dataLen=*(int*)(dataPtr);
      dataPtr+=4;
      nowAt+=4;
      if(0>(srcDataLen-=dataLen+minSize))
        {
          return list;
        }
      mEMSData.setSourceData(QByteArray(dataPtr,dataLen));
      dataPtr+=dataLen;
      nowAt+=dataLen;
      if((DateTime&0xff)!=*dataPtr)
        {
          mEMSData.setSourceData(QByteArray());
          return list;
        }
      ++dataPtr;
      ++nowAt;
      qint64 UTC=*(qint64*)dataPtr;
      dataPtr+=sizeof (qint64);
      nowAt+=sizeof (qint64);
      QDateTime dateTime= QDateTime::fromSecsSinceEpoch(UTC);
      if(!dateTime.isValid())
        {
          mEMSData.setSourceData(QByteArray());
          return list;
        }
      mEMSData.setCreateTime(dateTime);
      list.append(mEMSData);
    }
  return list;
}

bool MEMSData::isValid() const
{
  return (!sourceData.isEmpty())&&(createTime.isValid());
}

QString MEMSData::toString() const
{
  return QString("MEMSData:{sourceData:{%1},creatTime:{%2}}").arg((QString)sourceData.toHex()).arg(createTime.toString("yyyy-MM-dd hh:mm:ss"));
}

QByteArray MEMSData::getSourceData() const
{
  return sourceData;
}

void MEMSData::setSourceData(const QByteArray sourceData)
{
  this->sourceData=sourceData;
}

QDateTime MEMSData::getCreateTime() const
{
  return createTime;
}

void MEMSData::setCreateTime(const QDateTime dateTime)
{
  this->createTime=dateTime;
}

MEMSData &MEMSData::operator=(const MEMSData &mEMSData)
{
  setSourceData(mEMSData.getSourceData());
  setCreateTime(mEMSData.getCreateTime());
  return *this;
}
